package feizhou.untils.tools.io;


import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfPage;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.canvas.parser.PdfTextExtractor;

import java.io.IOException;


public class PDFUtil {





    /**
     * 读取pdf文件的内容
     *
     * @param filename F:/data/aet618.pdf
     * @return String
     */
    public static String readPDF(String filename) {

        StringBuilder result = new StringBuilder();

        PdfReader reader = null;
        try {
            reader = new PdfReader(filename);
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 1、创建PDF文档对象
        PdfDocument pdfDocument = new PdfDocument(reader);
        // 获取总页数
        int countPage = pdfDocument.getNumberOfPages();
        for (int i = 1; i <= countPage; i++) {
            PdfPage page = pdfDocument.getPage(i);
            //读取text文本
            String textContent = PdfTextExtractor.getTextFromPage(page);
            result.append(textContent);
        }
        // 关闭文档
        pdfDocument.close();


        return result.toString();
    }


    public static void main(String[] args) {
        String path = "C:\\Users\\26372\\Desktop\\temp\\test\\222.pdf";
        String outPath = "C:\\Users\\26372\\Desktop\\temp\\test\\222.txt";
        IOStreamUtil.write_bufferedWriter(readPDF(path), outPath);


    }



}
